/*codeWeb默认函数*/

/*============================== start ==============================*/

class CodeWeb {

  /*禁用右键、选择*/
  static disableReplication() {
    this.$nextTick(() => {
      // 禁用右键
      document.oncontextmenu = new Function("event.returnValue=false");
      // 禁用选择
      document.onselectstart = new Function("event.returnValue=false");
    });
  }

  /*获取对应类型、对应时间的对应数字值*/
  static getDateToKey(date, key, split) {
    date = date == null ? new Date() : date;
    const year = date.getFullYear().toString().substr(-4);
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    switch (key) {
      case 'year':
        return `${year}`;
      case 'month':
        return split ? `${year}` + split + `${month}` : `${year}${month}`;
      case 'day':
        return split ? `${year}` + split + `${month}` + split + `${day}` : `${year}${month}${day}`;
      case 'hour':
        return split ? `${year}` + split + `${month}` + split + `${day} ${hour}` : `${year}${month}${day}${hour}`;
      case 'minute':
        return split ? `${year}` + split + `${month}` + split + `${day} ${hour}:${minute}` : `${year}${month}${day}${hour}${minute}`;
      case 'second':
      default:
        return split ? `${year}` + split + `${month}` + split + `${day} ${hour}:${minute}:${second}` : `${year}${month}${day}${hour}${second}`;
    }
  }

  /*数字显示为千位制*/
  static toThousands(num) {
    let numStr = (num || 0).toString();
    return numStr.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  }

  /*分页页数数组*/
  static pageNumsCount(total, pageNum, pageSize, pageNumsSize) {
    //总页数为总条数除页量有余进一
    let pageNums = [...new Array(Math.ceil(total / pageSize)).keys()]
    //总页数大于 pageNumsSize 当前页大于 pageNumsSize/2 开始截取总页数显示为 当前页 -pageNumsSize/2 -1  （下标多减1）到当前页 +pageNumsSize/2
    if (pageNums.length > pageNumsSize && pageNum > pageNumsSize / 2) {
      //splice(start,count) 函数是 从 start 下标开始截取共 count 个数
      pageNums = pageNums.splice(pageNum - pageNumsSize / 2, pageNumsSize);
    } else {
      pageNums = pageNums.splice(0, pageNumsSize)
    }
    return pageNums;
  }

}

export default CodeWeb;

/*============================== end ==============================*/
